const operationUnauthorized = (err, req, res, next) => {
    if(err.status === 401) {
        return res.send({
            code: 401,
            message: '操作失败',
            data: { errMessage: '身份认证错误，没有有效token' }
        })
    }
    next()
}

const operationFailed = (req, res, next) => {
    res.operationFailed = (err, code = 201) => {
        res.send({
            code,
            message: '操作失败',
            data: err instanceof Error ? err.message : err,
        })
    }
    next()
}

const operationSuccess = (req, res, next) => {
    res.operationSuccess = (status = 'failure', errCode = '', message = '', results = []) => {
        let data
        status !== 'failure' ? data = { status: status, message: message, results: results } : data = { status: 'failure', errCode: errCode, errMessage: message, results: results }
        res.send({
            code: 200,
            message: '操作成功！',
            data: data,
        })
    }
    next()
}

export default {
    operationUnauthorized,
    operationFailed,
    operationSuccess,
}